<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['from_pluginname']){
	$_SESSION['from_pluginname']=$_RQ['from_pluginname'];
}
$return=array();
if($_RQ['op']=='ewm'){
	//exi('开发中！','error');
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$id));
		if(!$wxapp){
			exi('指定小程序不存在！','error');
		}
		if(!$wxapp['appid'] || !$wxapp['appsecret']){
			exi('公众号参数缺失，无法生成小程序二维码！','error');
		}
		$action=trim($post['action']);
		if(!$action){
			exi('请输入页面action','error');
		}
		$action_array=links($action);
		$action_array['action']=str_replace('.','/',$action_array['action']);
		$attachment=pdo_getcolumn('core_account_wxapp_ewm',array('wxapp_id'=>$id,'action'=>$action),'attachment');
		if(!$attachment){
			//$action_dir=str_replace('.','/',$action);
			$page="/{$action_array['plugin']}/{$action_array['action']}/index";
			if($action_array['query']){
				$page.="?".$action_array['query'];
			}
			$data=model('core/wxapp')->appcode($page,$wxapp['appid'],$wxapp['appsecret']);
			$attachment=model('core/files')->source_save($data,'data','jpg');
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'wxapp_id'=>$id,
				'action'=>$action,
				'attachment'=>$attachment,
			);
			pdo_insert('core_account_wxapp_ewm',$insert);
		}
		$ewm_url=tomedia($attachment);
		exi('','','',$ewm_url);
	}
	$id=intval($_RQ['id']);
	$item=array(
		'id'=>$id,
	);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'link','name'=>'action','from'=>'action','title'=>'页面action','help'=>'根据url生成不同的小程序码，填写标准完整的页面action'),
		),
	);
	exi($return);
}
if($_RQ['op']=='edit'){
	//新增编辑小程序
	$tabs=array(
		array('op'=>'edit','title'=>'小程序基本设置'),
	);
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'title'=>$post['title'],
			'desc'=>$post['desc'],
			'logo'=>$post['logo'],
			'appid'=>$post['appid'],
			'appsecret'=>$post['appsecret'],
			'oldid'=>$post['oldid'],
			'setting'=>ijson_encode($post['setting']),
			'sharetitle'=>trim($post['sharetitle']),
			'sharedesc'=>trim($post['sharedesc']),
			'shareimg'=>trim($post['shareimg']),
		);
		if($id){
			$data['wxapp_id']=pdo_getcolumn('core_wxapp',array('name'=>$_SESSION['from_pluginname']),'id');
			pdo_update('core_account_wxapp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['type']=2;
			$data['wxapp_id']=pdo_getcolumn('core_wxapp',array('name'=>$_SESSION['from_pluginname']),'id');
			$data['uniacid']=$_SESSION['uniacid'];
			$data['plugin_name']=$_SESSION['from_pluginname'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_wxapp',$data);
		}
		exi('操作成功！');
	}
	$item=pdo_get('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>$_SESSION['from_pluginname'],'type'=>2),array('id','title','logo','appid','appsecret','oldid','setting','sharetitle','sharedesc','shareimg','plugin_name'));
	if(empty($item)){
		$item=array();
		$wxappinfo=pdo_get('core_wxapp',array('name'=>$_SESSION['from_pluginname']),array('title','image','name'));
		$item['title']=$wxappinfo['title'];
		$item['logo']=$wxappinfo['image'];
		$item['plugin_name']=$_SESSION['from_pluginname'];
		$item['wxapp_id']=intval($_RQ['wxapp_id']);
	}else{
		$tabs[]=array('action'=>'core/wxapp.from_plugin/up_desc/id='.$item['id'],'title'=>'发布小程序');
	}
	
	$return['item']=$item;
	$setting =$return['item']['setting'] ? json_decode($return['item']['setting'],true):array('isfooter'=>0);
	$setting['window']=$setting['window']?$setting['window']:array('navigationStyle'=>'');
	$setting['footer']=$setting['footer']?$setting['footer']:array('background'=>'#fff');
	$setting['footer']['menus']=$setting['footer']['menus']?$setting['footer']['menus']:array();
	$setting['window']['navigationBarTitleText']=$setting['window']['navigationBarTitleText']?$setting['window']['navigationBarTitleText']:$return['item']['title'];
	$setting['window']['navigationBarBackgroundColor']=$setting['window']['navigationBarBackgroundColor']?$setting['window']['navigationBarBackgroundColor']:'#f10215';
	$setting['window']['navigationBarTextStyle']=$setting['window']['navigationBarTextStyle']?$setting['window']['navigationBarTextStyle']:'white';
	$return['item']['setting'] = $setting;
	$return['item']['news_num']=$return['item']['shownums']['news_num'];
	$return['item']['stores_num']=$return['item']['shownums']['stores_num'];
	$return['item']['products_num']=$return['item']['shownums']['products_num'];
	$return['item']['anlis_num']=$return['item']['shownums']['anlis_num'];
	$return['navigationBarTextStyles'] =array('white'=>'white','black'=>'black');
	$return['backgroundTextStyles'] =array('dark'=>'dark','light'=>'light');
	$return['borderStyles'] =array('white'=>'white','black'=>'black');
	$return['positions'] =array('bottom'=>'bottom','top'=>'top');
	$return['navigationStyles'] =array('default'=>'default','custom'=>'custom');
	$return['iconstyle'] =array('0'=>'图片图标','1'=>'字体图标');
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置小程序基本参数',
		'tabs'=>$tabs,
		'edit_tab'=>true,
			'edits'=>array(
					array(
					'title'=>'基本信息','list'=>array(
						array('type'=>'string','name'=>'title','title'=>'小程序名称'),
						array('type'=>'image','name'=>'logo','title'=>'小程序logo'),
						array('type'=>'string','title'=>'appid','name'=>'appid','help'=>'填写小程序appid' ),
						array('type'=>'string','title'=>'appsecret','name'=>'appsecret','help'=>'填写小程序 appsecret' ),
						array('type'=>'string','title'=>'原始ID','name'=>'oldid','help'=>'填写小程序原始ID' ),
						),
					),
					array(
					'title'=>'顶部导航设置','list'=>array(
						array('type'=>'radio','name'=>'setting.window.navigationStyle','title'=>'是否关闭顶部导航','options'=>'navigationStyles','option_value'=>'dtitle','help'=>'导航栏样式，仅支持以下值：default 默认样式custom 自定义导航栏，只保留右上角胶囊按钮'),
						array('type'=>'color','name'=>'setting.window.navigationBarBackgroundColor','title'=>'导航栏背景色','help'=>'小程序顶部背景色'),
						array('type'=>'radio','name'=>'setting.window.navigationBarTextStyle','title'=>'导航栏标题颜色','options'=>'navigationBarTextStyles','option_value'=>'dtitle'),
						array('type'=>'string','name'=>'setting.window.navigationBarTitleText','title'=>'导航栏标题','help'=>'导航栏标题文字内容！留空这使用小程序名称'),
					),
					),
					array(
						'title'=>'窗口样式设置','list'=>array(
							array('type'=>'color','name'=>'setting.window.backgroundColor','title'=>'窗口的背景色','help'=>'默认白色，大部分不用填写！'),
							array('type'=>'radio','name'=>'setting.window.backgroundTextStyle','title'=>'窗口的背景色','options'=>'backgroundTextStyles','option_value'=>'dtitle'),
							array('type'=>'color','name'=>'setting.window.backgroundColorTop','title'=>'顶部窗口的背景色','help'=>'顶部窗口的背景色，仅 iOS 支持,微信版本 6.5.16'),
							array('type'=>'color','name'=>'setting.window.backgroundColorBottom','title'=>'底部窗口的背景色','help'=>'顶部窗口的背景色，仅 iOS 支持,微信版本 6.5.16'),
						),
					),
					array(
						'title'=>'分享设置','list'=>array(
							array('type'=>'string','name'=>'sharetitle','title'=>'小程序分享标题','help'=>'小程序分享标题'),
							array('type'=>'string','name'=>'sharedesc','title'=>'小程序分享描述','help'=>'小程序首页分享描述'),
							array('type'=>'image','name'=>'shareimg','title'=>'小程序分享图片','help'=>'小程序首页分享图片自定义，留空则默认截屏分享！'),
						),
					),
					array(
						'title'=>'其他全局设置','list'=>array(
							array('type'=>'switch','name'=>'setting.window.enablePullDownRefresh','title'=>'是否全局开启下拉刷新'),
							array('type'=>'string','name'=>'setting.window.onReachBottomDistance','title'=>'上拉触底距离','help'=>'只能填数字，默认50，页面上拉触底事件触发时距页面底部距离，单位为px'),
						),
					),
		),
	);
	exi($return);
}
if($_RQ['op']=='up_desc'){
	$cloud=core_setting('cloud');
	//设置上传参数
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['id']){
			exi('请先选择小程序！','error');
		}
		if(!$post['newversion']){
			exi('请先设置上传版本！','error');
		}
		if(!$post['desc']){
			exi('请先设置版本说明！','error');
		}
		$_SESSION['wxapp_up']=array(
			'id'=>intval($post['id']),
			'version'=>$post['newversion'],
			'desc'=>$post['desc'],
			'plugin_name'=>$_SESSION['plugin_name'],
		);
		exi('','','core/wxapp.diy_up/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择小程序！','error');
	}
	$item=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','type','version'));
	$groupid = $cloud['group'] ? $cloud['group'] : '0';
	if(!$groupid && $item['type'] !=2){
		exi('您不能上传DIY小程序，请联系管理员开通！','error');
	}
	$item['version']=$item['version']?$item['version']:'1.0';
	$item['newversion']=$item['version'] + 0.1;
	$item['version']='V-'.$item['version'];
	$item['newversion']='V-'.$item['newversion'];
	$time = date('Y-m-d H:i:s',TIMESTAMP);
	$item['desc']=$item['desc']?$item['desc']:'新版本:'.$time.'发布！版本号：'.$item['newversion'];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
				array('op'=>'edit','title'=>'返回小程序'),
			),
		'edit'=>array(
			array('type'=>'string','name'=>'version','title'=>'当前版本','read'=>true),
			array('type'=>'string','name'=>'newversion','title'=>'新版本'),
			array('type'=>'string','name'=>'desc','title'=>'说明'),
		),
	);
	exi($return);
}
